package com.eastdigit.system.controller;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eastdigit.servlet.ReqBean;
import com.eastdigit.servlet.RequestAttribute;
import com.eastdigit.servlet.Response;
import com.eastdigit.system.service.WuhanBusUserService;

@Controller
@RequestMapping(value = "/sync")
public class SyncDeptAndUserController {

    
    @Autowired
    private WuhanBusUserService wuhanBusUserService;
    
    public static long lastRefreshUsersTimes = 0;
    public static long lastRefreshDeptsTimes = 0;
    /**
     * 刷新用户
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/user/refresh")
    @ResponseBody
    public JSONObject refreshUsers(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
    	int minite = 1;
    	if(System.currentTimeMillis() - lastRefreshUsersTimes >= minite*60*1000){
    		lastRefreshUsersTimes = System.currentTimeMillis();
    		wuhanBusUserService.syncUsers();
            return Response.responseSuccess();
    	}else{
    		long i = minite - (System.currentTimeMillis() - lastRefreshUsersTimes)/60/1000;
    		return Response.responseError("请在" + i + "分钟后执行此操作");
    	}
    }
    
    /**
     * 刷新部门
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/department/refresh")
    @ResponseBody
    public JSONObject refreshDept(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
    	int minite = 1;
    	if(System.currentTimeMillis() - lastRefreshDeptsTimes >= minite*60*1000){
    		lastRefreshDeptsTimes = System.currentTimeMillis();
    		wuhanBusUserService.syncDepts();
            return Response.responseSuccess();
    	}else{
    		long i = minite - (System.currentTimeMillis() - lastRefreshDeptsTimes)/60/1000;
    		return Response.responseError("请在" + i + "分钟后执行此操作");
    	}
    }

}
